﻿using System.Text;
using MicroBlog.Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace MicroBlog.Areas.Admin.Controllers
{
    /// <summary>
    /// 控制器的基类
    /// </summary>
    [Area("Admin")]
    [BlogExceptionFilter]
    [Authorize(Policy = App.AuthPolicy)]
    public abstract class BaseController : Controller
    {
        /// <summary>
        /// 获取所有数据模型验证错误信息
        /// </summary>
        /// <param name="modelStateDictionary"></param>
        /// <returns></returns>
        protected string GetModelErrors(ModelStateDictionary modelStateDictionary)
        {
            StringBuilder errors = new StringBuilder();
            foreach (var pair in modelStateDictionary)
            {
                foreach (ModelError error in pair.Value.Errors)
                {
                    errors.Append(error.ErrorMessage);
                }
            }
            return errors.ToString();
        }
    }
}